<template>
  <el-dialog :title="text.type" :visible.sync="isShow" :width="text.modal?'600px':'400px'" :show-close="false" :close-on-click-modal="false">
      <table style="width:100%">
          <tr>
              <td colspan="3">
                  <div style="width: 100%;color: #FA8363;text-indent: 2em;padding: 10px;" v-if="!!text.optiontxt" v-html="text.optiontxt"></div>
              </td>
          </tr>
          <tr v-show="text.alertflag" style="height:40px">
              <td align="right"></td>
              <td >
                  <el-checkbox ref="loginalertflag" v-model="loginalertflag" true-label="1" false-label="0" style="margin-left:5px;">危急值</el-checkbox>
              </td>
          </tr>
          <tr>
              <td v-show="text.modal">
                  <div style="width: 300px;border-right: 1px solid #ccc;color: #FA8363" v-show="text.modal">
                      本软件在假定检验数据真实准确的前提下，仅对检验数据遵循设定的审核规则进行自动审核判定，请您在启用此功能前仔细核对审核规则！本软件不承担由此产生的任何法律责任！
                      <br>
                      如您登录确认启用此功能，视为您知晓并同意使用审核规则，清楚自动审核报告产生的风险并自愿承担相应的责任！
                  </div>
              </td>
              <td></td>
              <td>
                  <div style="color: #FA8363;" v-show="text.tipmsg">{{text.tipmsg}}</div>
                  <div>
                      <el-form label-width="140px">
                          <el-form-item label="帐 号" :label-width="formLabelWidth">
                              <el-input v-model.trim="userid" placeholder="请输入帐号" ref="refuserid" :disabled="text.changeflag" @keyup.enter.native="chkenter('refuserid')"><template slot="prepend"><i class="el-icon-info"></i></template></el-input>
                          </el-form-item>
                          <el-form-item label="姓 名" :label-width="formLabelWidth">
                              <div>
                                  <div style="float:left;background:#F5F7FA;width:54.44px;border:1px solid #ccc;text-align:center;height:25.99px;"><i class="el-icon-user-solid" style="line-height: 26px !important;vertical-align: top;"></i></div>
                                  <div style="border:1px solid #DCDFE6;height:25.99px;display: block;">
                                      <span style="line-height: 26px !important;vertical-align: top;padding-left:10px;font-size:12px;top:0px">{{username}}</span>
                                  </div>
                              </div>
                              <!-- <el-input v-model.trim="username" placeholder="请输入姓名" readonly><template slot="prepend"><i class="el-icon-user-solid"></i></template></el-input> -->
                          </el-form-item>
                          <el-form-item label="密 码" :label-width="formLabelWidth">
                              <el-input v-model.trim="userpwd" type="password" ref="refuserpwd" @keyup.enter.native="chkenter('refuserpwd')" placeholder="请输入密码"><template slot="prepend"><i class="el-icon-key"></i></template></el-input>
                          </el-form-item>
                          <el-form-item v-if="text.deletesamplemustreason == '1'" label="原 因" :label-width="formLabelWidth">
                              <el-input v-model.trim="rejectreason" ref="rejectreason" @keyup.enter.native="chkenter('rejectreason')" placeholder="请输入原因"><template slot="prepend"><i class="el-icon-chat-line-square"></i></template></el-input>
                          </el-form-item>
                      </el-form>
                  </div>
              </td>
          </tr>
          <tr>
              <td colspan="3" style="color: red;">{{rejectreasontip}}</td>
          </tr>
      </table>

      <div style="display:none">
          <button type="button" @click="isLogSuccess">
              <span>
                  {{text.btn.confirmBtnText}}
              </span>
          </button>
      </div>
      <span slot="footer" class="dialog-footer">
          <el-button @click="confirm" ref="confirmid" @keyup.enter.native="chkenter('confirmid')" @keyup.right.native="chkbtnfun('confirmid')">确 定</el-button>
          <el-button @click="cancel" @keyup.left.native="chkbtnfun('btnfocus1')">取 消</el-button>
      </span>
  </el-dialog>
</template>

<script type="text/javascript">
import md5 from "js-md5";
export default {
  data() {
      return {
          isShow: true,
          text: {
              type: "用户登录验证",
              modal: false,
              powerkind: "", //权限类型
              powerobjid: "", //权限ID
              btn: {
                  confirmBtnText: "确定",
                  cancelBtnText: "取消"
              },
              userid: "",
              username: "",
              optiontxt: "",
              changeflag: false,
              deletesamplemustreason: "0",
              alertflag:false,
              tipmsg:"",
          },
          userid: "",
          username: "",
          userpwd: "",
          formLabelWidth: "50px",
          logflag: "0",
          rejectreason: "",
          rejectreasontip: "",
          loginalertflag:"0",
      };
  },
  watch: {
      userid(newVal) {
          if (newVal != "") {
              this.findname(newVal);
          } else {
              this.username = "";
          }
      }
  },
  mounted() {
      this.$nextTick(() => {
          if (!this.text.changeflag) {
              setTimeout(() => {
                  this.$refs.refuserid.focus();       
              }, 500);
          } else {
              this.userid = this.text.userid;
              this.username = this.text.username;
              setTimeout(() => {
                  this.$refs.refuserpwd.focus(); 
              }, 500);
          }
      });
  },
  updated() {},
  methods: {
      chkenter(obj) {
          if (obj == "refuserid") {
              this.$refs.refuserpwd.focus();
          } else if (obj == "refuserpwd") {
              if (this.text.deletesamplemustreason != "1") {
                  setTimeout(() => {
                      this.$refs.confirmid.$el.focus();
                  }, 100);
              } else {
                  this.$refs.rejectreason.focus();
              }
          } else if (obj == "rejectreason") {
              this.confirm();
          }
      },
      chkbtnfun(obj) {
          if (obj == "btnfocus1") {
              setTimeout(() => {
                  this.$refs.confirmid.$el.focus();
              }, 100);
          } else {
              setTimeout(() => {
                  this.$refs.btnfocus1.$el.focus();
              }, 100);
          }
      },
      isLogSuccess() {},
      cancel() {},
      confirm() {
          if (this.text.deletesamplemustreason == "1" && !this.rejectreason) {
              this.rejectreasontip = "请输入撤销登记原因!";
              return false;
          }
          this.$tool
              .request({
                  url: "/ucent/login",
                  method: "post",
                  data: {
                      action: "Nlogin",
                      password: md5("rms-" + this.userpwd + "-rme"),
                      userno: this.userid, //this.username
                      powerkind: this.text.powerkind,
                      powerobjid: this.text.powerobjid
                  }
              })
              .then(resp => {
                  if (resp.flag != 1) {
                      //登录失败TODO
                      this.isShow = true;
                      this.logflag = resp.flag;
                      this.$alert("登录失败！！" + resp.msg, "错误", {
                          confirmButtonText: "确定"
                      });
                  } else {
                      //登录成功TODO
                      this.isShow = false;
                      this.logflag = resp.flag;
                      this.userid = resp.userno; //  使用后台查询到的用户代码, 保证大小写一致
                      this.isLogSuccess();
                      // this.$message({
                      //     type: "info",
                      //     message: "登录成功！！"
                      // });
                  }
              })
              .catch(e => {
                  console.log("error", e);
              });
      },
      findname(newVal) {
          this.$tool
              .request({
                  url: "ucent/ddl",
                  method: "post",
                  data: {
                      action: "loginname",
                      userid: newVal
                  }
              })
              .then(res => {
                  this.username = res.name;
              })
              .catch(e => {
                  console.log("error", e);
              });
      }
  }
};
</Script>
